<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #box1 {
        width: 100px;
        height: 100px;
        background-color: red;

        position: absolute;
      }
      body {
        overflow: auto;
      }
    </style>
    <script>
      window.onload = function () {
        /**
         *div跟随鼠标移动
         */
        var box1 = document.getElementById("box1");
        document.onmousemove = function (event) {
          event = event || window.event;
          //获取鼠标的坐标
          var left = event.clientX;
          var top = event.clientY;

          box1.style.left = left + "px";
          box1.style.top = top + "px";
        };
      };
    </script>
  </head>
  <body style="height: 1000px; width: 200px">
    <div id="box1"></div>
  </body>
</html>
